package org.mtmi.ui.drawables;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.mtmi.ui.scenes.CLR;
import org.mtmi.ui.scenes.COLOR;

/**
 * Display a color in a square.
 */
public class ColorDrawable extends AbstractColorDrawable 
{
	private boolean gradient;
	
	/** caller is responsible to dispose color */
	public ColorDrawable( Color clr, boolean _gradient ) {
		super( clr );
		gradient = _gradient;
	}
	
	@Override
	public void paint( GC gc, int x, int y, int width, int height, int state ) {
		//a square:
		int r = Math.min( width, height );
		x = x+(width-r)/2;
		y = y+(height-r)/2;
		
		Color cl = CLR.lighter( color, 0.3f );
		Color cd = CLR.darker( color, 0.3f );
		if( gradient ) {
			cl = CLR.lighter( color, 0.3f );
			cd = CLR.darker( color, 0.3f );
		}
		if( r >= 14 ) {
			if( state==SELECTED ) {
				gc.setBackground( COLOR.grey100 );
			} else {
				gc.setBackground( COLOR.grey70 );
			}
			gc.fillRectangle( x, y, width, height );
			gc.setBackground( COLOR.grey20 );
			int r4=r-4;
			gc.fillRectangle( x+2, y+2, r4, r4 );
			int r8 = r-8;
			if( gradient ) {
				gc.setForeground( cl );
				gc.setBackground( cd );
				gc.fillGradientRectangle( x+4, y+4, r8, r8, true );
			} else {
				gc.setBackground( color );
				gc.fillRectangle( x+4, y+4, r8, r8 );
			}
		} else {
			if( gradient ) {
				gc.setForeground( cl );
				gc.setBackground( cd );
				gc.fillGradientRectangle( x, y, r, r, true );
			} else {
				gc.setBackground( color );
				gc.fillRectangle( x, y, r, r );
			}
		}
		if( cl!=null ) cl.dispose();
		if( cd!=null ) cd.dispose();
	}

}
